package com.admgt.util.progressbar;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

/**
 * Show the upload and down load details.
 * With a table and process bar in it.
 */
public class LoadDetails {

	public static void main(String[] args){
		TestFrame frame= new TestFrame();
		frame.setVisible(true);
	}
}
class TestFrame extends JFrame{
	private static final long serialVersionUID = 1L;
	private ProgressTableModel progressTableModel;
	private ProgressBarObservable progressBarObservable;
	private ProgressBarObserver progressBarObserver;


	public TestFrame(){
		init();
	}
	private void init() {
		setTitle("Process Bar test");
		setSize(Toolkit.getDefaultToolkit().getScreenSize().width/2,
				Toolkit.getDefaultToolkit().getScreenSize().height/2+30);
		setContentPane(createContentpane());
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
			
		});
		progressBarObservable = new ProgressBarObservable();
		progressBarObserver = new ProgressBarObserver();
		progressBarObservable.addObserver(progressBarObserver);
	}

	private Container createContentpane() {
		JPanel pane =  new JPanel(new BorderLayout());
		pane.add(BorderLayout.CENTER,createTablePane());
		pane.add(BorderLayout.SOUTH,createBtnPane());
		return pane;
	}
	private Component createTablePane() {
		progressTableModel = new ProgressTableModel();
		JTable statusTable = new JTable(progressTableModel);
		//设置"Status"列由定制的ProgressBarRenderer渲染
		TableColumn statusColumn  = statusTable.getColumn("进度");
		statusColumn .setCellRenderer(new ProgressBarRenderer());
		JScrollPane scrollPane = new JScrollPane(statusTable);
		return scrollPane;
	}
	private Component createBtnPane() {
		JPanel btnPane =  new JPanel(new BorderLayout());
		JButton addButton = new JButton("添加");
		addButton.setBounds(WIDTH/2 -40, 350 , 80, 20);
		addButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				progressTableModel.addProcessBar();
				Thread t= new ProgressBarThread();
				t.start();

			}
		});
		JPanel p =  new JPanel();
		p.add(addButton);
		btnPane.add(BorderLayout.CENTER,p);
		return btnPane;
	}


	public class ProgressBarThread extends Thread {
		int price =0;
		int rowIndex = progressTableModel.getRowCount()-1;
		
		public void run(){
			while(price<100){
				try{
					Thread.sleep(100);
				}catch(InterruptedException e){
					e.printStackTrace();
				}
				price++ ;
				SwingUtilities.invokeLater(new Runnable(){
					public void run(){
						progressBarObservable.setShowValue(rowIndex+":"+price);
					}
				});
				
			}
		}
	}
	

	public class ProgressBarObserver implements Observer{
		@Override
		public void update(Observable o, Object arg) {
				String showValue = (String)arg;
				System.out.println("value = "+showValue.toString());
				progressTableModel.setValueAt(
						Integer.parseInt(showValue.split(":")[1]),
						Integer.parseInt(showValue.split(":")[0]),
						6);
		}
	}
	
	public class ProgressBarObservable extends Observable{
		private String showValue;
		public String getShowValue(){
			return showValue;
		}
		public void setShowValue(String showValue) {
			this.showValue = showValue;
			setChanged();
			notifyObservers(showValue);
		}
		
	}
	

	public class ProgressBarRenderer extends DefaultTableCellRenderer {
		private static final long serialVersionUID = 1L;

		private final JProgressBar bar;
		
		public ProgressBarRenderer() {
			super();
			setOpaque(true);
			bar =  new JProgressBar();
			bar.setStringPainted(true);
			bar.setMaximum(100);
			bar.setMinimum(0);
			bar.setBorderPainted(true);
			bar.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
		}
		@Override
		public Component getTableCellRendererComponent(JTable table, Object value,
				boolean isSelected, boolean hasFocus, int row, int column) {
			Integer n = (Integer)value;
			bar.setValue(n);
			return bar;
		}
		

	}


}
